class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        if (obstacleGrid.empty()) {
            return 0;
        }
        int _size = obstacleGrid[0].size();
        vector<int> res(_size, 0);
        res[0] = 1;
        for (int i = 0; i < obstacleGrid.size(); i++) {
            for (int j = 0; j < _size; j++) {
                if (obstacleGrid[i][j] == 1) {
                    res[j] = 0;
                } else if (j > 0) {
                    res[j] = res[j - 1] + res[j];
                }
            }
        }
        return res[_size - 1];
    }
};